import { getSidebarStatus,setSidebarStatus, setLanguage } from '@/utils/cookies'
import { getLocale } from '@/lang/index'
interface State {
  sidebar: {
    opened: boolean;
  };
	language: string
}

interface Mutations<S = State> {
  MUTATIONS_TOGGLE_SIDEBAR (state: S): void
  MUTATIONS_SET_LANGUAGE (state: S,language: string): void
}

interface ActionContext {
  commit<K extends keyof Mutations>(
      key: K,
      payload ?: Parameters<Mutations[K]>[1],
  ): ReturnType<Mutations[K]>
}

interface Actions {
  ACTION_TOGGLESIDEBAR ({ commit }: ActionContext): void
  ACTION_SET_LANGUAGE ({ commit }: ActionContext,language: string): void
}

const state: State = {
	sidebar: {
		opened: getSidebarStatus() !== 'closed',
	},
	language: getLocale() || 'zh-cn',
};

const mutations: Mutations = {
	MUTATIONS_TOGGLE_SIDEBAR: (state) => {
		state.sidebar.opened = !state.sidebar.opened;
		if (state.sidebar.opened) {
      setSidebarStatus('opened')
    } else {
      setSidebarStatus('closed')
    }
	},
	MUTATIONS_SET_LANGUAGE: (state, language) => {
		state.language = language;
		setLanguage(language)
	}
};

const actions: Actions = {
	ACTION_TOGGLESIDEBAR ({ commit }) {
		commit('MUTATIONS_TOGGLE_SIDEBAR');
	},
	ACTION_SET_LANGUAGE ({ commit }, language: string) {
		commit('MUTATIONS_SET_LANGUAGE',language);
	},
};

export default {
	namespaced: true,
	state,
	mutations,
	actions
}